<?php
/* 
 * akimimi front
 * 
 * delivery_info Model
 * 
 * @author steve
 * 
 */
require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class DeliveryInfoDao extends BaseDao {
	
	private $tableName = "delivery_infos";
	
	public function getDeliveriesOfUser($userId){
		$param = array($userId);
		$sql = "select * from $this->tableName where user_id = ? and is_delete = 0 ";
        return $this->dbUtil->SelectData($sql, $param);
	}
	
	public function saveDelivery($param){
		$sql = "insert into $this->tableName (user_id, name, address_1, address_2, address_3,detail_address,phone_number_1,phone_number_2,email, created, modified) values (?,?,?,?,?,?,?,?,?,now(),now())";
        return $this->dbUtil->ExecuteQuery($sql, $param);
	}
	
	public function getDeliveryById($deliveryId){
		$param = array($deliveryId);
		$sql = "select * from $this->tableName where id = ? and is_delete = 0 ";
        return $this->dbUtil->SelectData($sql, $param);
	}
	
	public function updateDelivery($param){
		$sql = "update $this->tableName set name = ?, address_1 = ?, address_2 = ?, address_3 = ?, detail_address = ?, phone_number_1 = ?, phone_number_2 = ?, email = ?, modified = now() where id = ? and is_delete = 0";
        return $this->dbUtil->ExecuteQuery($sql, $param);
	}
	
	public function delDelivery($deliveryId){
		$param = array($deliveryId);
		$sql = "update $this->tableName set is_delete = 1 where id = ? ";
        return $this->dbUtil->ExecuteQuery($sql,$param);
	}
	
	
	/**
	 * allen add
	 * get the top 5 address for checkout View
	 */
    public function getAddressByCid($customerId){
        $param = array($customerId);
        $sql = "select * from  $this->tableName where user_id = ? and is_delete = 0 group by modified desc , id desc limit 0, 5 ";
        return $this->dbUtil->SelectData($sql, $param);
    }
    
    /**
     * get the address number by user id 
     */
    public function getAddressNumByUserId($userId){
        $param = array($userId);
        $sql = "select count(*) from  $this->tableName where user_id = ? and is_delete = 0 ";
        return $this->dbUtil->SelectData($sql, $param);
    }    
    
    /**
     * get the address info by id 
     */
    public function getAddressInfoById($id){
        $param = array($id);
        $sql = "select * from  $this->tableName where id = ? and is_delete = 0 ";
        return $this->dbUtil->SelectData($sql, $param);
    }     
	
}
